//同意实名认证协议
$(function () {
    $("#agree").click(function () {
        var ischeck = document.getElementById("agree").checked;
        if (ischeck) {
            $("#btnRegist").attr("disabled", false);
            $("#btnRegist").removeClass("fail");
        } else {
            $("#btnRegist").attr("disabled", "disabled");
            $("#btnRegist").addClass("fail");
        }
    });

    //realName 处理
    $("#realName").on("blur", function () {
        var name = $.trim($("#realName").val());
        if ("" == name) {
            showError("realName", "必须输入姓名");
        } else if (name.length < 2) {
            showError("realName", "必须输入正确的姓名");
        } else if (!/^[\u4e00-\u9fa5]{0,}$/.test(name)) {
            showError("realName", "姓名必须是中文的");
        } else {
            showSuccess("realName");
        }
    })

    //realName 处理完成

    //身份证号
    $("#idCard").on("blur", function () {
        var card = $.trim($("#idCard").val());
        if ("" == card) {
            showError("idCard", "必须输入身份证号");
        } else if (!((card.length != 15) || (card.length != 18))) {
            showError("idCard", "身份证号必须是15或18位的")
        } else if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card)) {
            showError("idCard", "身份证号格式不正确");
        } else {
            showSuccess("idCard");
        }
    });

    //实名认证按钮
    $("#btnRegist").on("click", function () {
        $("#idCard").blur();
        $("#realName").blur();


        var text = $("div[id$='Err']").text();
        if ("" == text) {
            var card = $.trim($("#idCard").val());
            var name = $.trim($("#realName").val());
            var phone = $.trim($("#phone").val());
            $.ajax({
                url: contextPath + "/loan/realName",
                type: "post",
                data: {
                    phone: phone,
                    card: card,
                    name: name
                },
                dataType: "json",
                success: function (resp) {
                    if (resp.code == 0) {
                        window.location.href = contextPath + "/loan/myCenter";
                    } else {
                        alert(resp.message);
                    }
                },
                error: function () {
                    alert("请稍后重试");
                }
            })
        }
    })
});
//打开注册协议弹层
function alertBox(maskid, bosid) {
    $("#" + maskid).show();
    $("#" + bosid).show();
}

//关闭注册协议弹层
function closeBox(maskid, bosid) {
    $("#" + maskid).hide();
    $("#" + bosid).hide();
}

//错误提示
function showError(id, msg) {
    $("#" + id + "Ok").hide();
    $("#" + id + "Err").html("<i></i><p>" + msg + "</p>");
    $("#" + id + "Err").show();
    $("#" + id).addClass("input-red");
}

//错误隐藏
function hideError(id) {
    $("#" + id + "Err").hide();
    $("#" + id + "Err").html("");
    $("#" + id).removeClass("input-red");
}

//显示成功
function showSuccess(id) {
    $("#" + id + "Err").hide();
    $("#" + id + "Err").html("");
    $("#" + id + "Ok").show();
    $("#" + id).removeClass("input-red");
}